package com.example.questionnaires.util;

import com.example.questionnaires.entity.User;
import com.example.questionnaires.exception.BusinessException;
import org.springframework.stereotype.Component;

@Component
public class UserHolder {
    private static final ThreadLocal<User> userThreadLocal = new ThreadLocal<>();

    public static void saveUser(User user) {
        userThreadLocal.set(user);
    }

    public static User getUser() {
        User user = userThreadLocal.get();
        if (user == null) {
            throw new BusinessException("用户未登录");
        }
        return user;
    }

    public static void removeUser() {
        userThreadLocal.remove();
    }
} 